﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Repositories;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using static Estsh.Core.Model.EnumUtil.WmsEnumUtil;
using Estsh.Core.Model.EnumUtil;

namespace Estsh.Core.Wms.Services
{
    public class MaterialService : BaseService<BaseEntity>, IMaterialService
    {
        private readonly IMaterialRepository repository;
        public MaterialService(IMaterialRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        /// <summary>
        /// 获取箱条码信息
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <returns></returns>
        public SetObjectDetail GetMaterialInfo(string cartonNo)
        {
            SetObjectDetail det = new SetObjectDetail();

            List<SysStock> MaterialInfo = repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:包装条码已被冻结，请检查!";
                return det;
            }
            if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                return det;
            }
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            det.type = "OK";
            det.wmsObjectDetail = MaterialInfo.ConvertAll(s => (object)s);
            return det;
        }


        /// <summary>
        /// 扫描条码
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <returns></returns>
        public SetObjectDetail ScanCode(string cartonNo)
        {
            SetObjectDetail det = new SetObjectDetail();

            int MaterialInfo = repository.ScanCode(cartonNo);

            det.type = "OK";
            return det;
        }

        public SetObjectDetail SpellCode(string cartonNo)
        {
            SetObjectDetail det = new SetObjectDetail();
            List<SysStock> MaterialInfo = repository.SpellCode(cartonNo);
            det.type = "OK";
            det.wmsObjectDetail = MaterialInfo.ConvertAll(s => (object)s);
            return det;
        }

        public SetObjectDetail TailBox(string cartonNo)
        {
            SetObjectDetail det = new SetObjectDetail();

            List<SysStock> MaterialInfo = new List<SysStock>() ;
            List<SysStock> GetInfo = repository.GetMaterialInfo(cartonNo);//得到条码信息
            if (GetInfo.Count > 0)
            {
                MaterialInfo = repository.TailBox(GetInfo[0].PartNo);
            }
            if (GetInfo.Count == 0)
            {
                MaterialInfo = repository.TailBox(cartonNo);
            }
            if (MaterialInfo.Count > 0)
            {
                det.type = "OK";
            }
            else
            {
                det.type = "NO";
            }

            det.wmsObjectDetail = MaterialInfo.ConvertAll(s => (object)s);
            return det;
        }

        /// <summary>
        /// 拆分 箱条码
        /// </summary>
        /// <param name="oldStock"></param>
        /// <param name="splitNum"></param>
        /// <param name="loginId"></param>
        /// <param name="splitCount"></param>
        /// <returns></returns>
        public SetObjectDetail CheckMaterialSplit(string cartonNo, int splitNum, string loginId, int splitCount)
        {

            SetObjectDetail det = new SetObjectDetail();
            //需要拆分的箱条码信息
            List<SysStock> MaterialInfo = repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:包装条码已被冻结，请检查!";
                return det;
            }
            if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                return det;
            }
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            List<SysStock> NewStock = repository.CheckMaterialSplit(MaterialInfo, splitNum, loginId, splitCount);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }

        /// <summary>
        /// 合并
        /// </summary>
        /// <param name="hostCartonNo"></param>
        /// <param name="SecondaryCartonNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public SetObjectDetail CheckMaterialConsolidation(string hostCartonNo, string SecondaryCartonNo, string loginId)
        {
            SetObjectDetail det = new SetObjectDetail();

            //主箱条码信息
            List<SysStock> host = repository.GetMaterialInfo(hostCartonNo);
            if (host.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:主箱条码不存在，请检查!";
                return det;
            }
            if (host[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:主箱条码已被冻结，请检查!";
                return det;
            }
            if (host[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:主箱条码不处于在库状态!";
                return det;
            }

            //副箱条码信息
            List<SysStock> Secondary = repository.GetMaterialInfo(SecondaryCartonNo);
            if (Secondary.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:副箱条码不存在，请检查!";
                return det;
            }
            if (Secondary[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:副箱条码已被冻结，请检查!";
                return det;
            }
            if (Secondary[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:副箱条码不处于在库状态!";
                return det;
            }
            //条码匹配检查
            if (!host[0].LocateId.Equals(Secondary[0].LocateId) || !host[0].WarehouseId.Equals(Secondary[0].WarehouseId) || !host[0].ZoneId.Equals(Secondary[0].ZoneId))
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:合并条码库位不一致，请检查!";
                return det;
            }
            if (!host[0].Status.Equals(Secondary[0].Status))
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:合并条码状态不一致，请检查!";
                return det;
            }
            if (!host[0].LotNo.Equals(Secondary[0].LotNo))
            {
                det.type = "NO";
                det.message = "[" + hostCartonNo + "]:合并条码批次不一致，请检查!";
                return det;
            }

            List<SysStock> NewStock = repository.CheckMaterialConsolidation(host, Secondary, loginId);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }
    }
}
